# -*- coding=utf8 -*-
__author__ = 'nickleo'

from beans.tree import TreeNode, TreeNodeGroup
import jsonpickle as json
from config import HogWartsConfig as cfg
import http_client as http
import time


# # 主要对应的页面为mq.html . 这个页面显示的结构如下
# # nameserver
##      ----topic1
##              ----brokerDatas
##              ----filterServerTable
##              ----queueDatas
##      ----topic2

##              ----brokerDatas
##              ----filterServerTable
##              ----queueDatas
##      ----topic3...
##


def tree():
    result = []
    action_alert = None
    for name_server in cfg().get_nameservers():
        print 'name_server'+name_server
        group = TreeNodeGroup("nameserver:" + name_server,'primary', name_server+str(time.time()))
        http_topiclist_result = http.get_topiclist(name_server)

        if http_topiclist_result is None or http_topiclist_result.strip() == '':
            result.append(group)
            action_alert = 'nameserver:' + name_server + 'is No TopicRoute info '
        else:
            for _topiclist in json.decode(http_topiclist_result):
                # _topiclist ={"ClusterName":"FetionMQCluster","Topic":"FetionPerformace","ConsumerGroup":"FetionPerformance"}
                topic = TreeNode(_topiclist['Topic'], 'primary', 'ClusterName:' + _topiclist['ClusterName'] + '\tConsumerGroup:' + _topiclist['ConsumerGroup'])
                http_topicroute_result = http.get_topicroute(name_server, _topiclist['Topic'])
                # {
                # "brokerDatas":[
                # 		{
                # 			"brokerAddrs":{0:"192.168.143.130:10911",1:"192.168.143.133:10911"
                # 			},
                # 			"brokerName":"fetionmq"
                # 		}
                # 	],
                # 	"filterServerTable":{},
                # 	"queueDatas":[
                # 		{
                # 			"brokerName":"fetionmq",
                # 			"perm":7,
                # 			"readQueueNums":1,
                # 			"topicSynFlag":0,
                # 			"writeQueueNums":1
                # 		}
                # 	]
                # }
                if http_topicroute_result is None or http_topicroute_result.strip() == '':
                    topic.desc = 'topic:' + _topiclist['Topic'] + 'is No TopicRoute info '
                else:
                    http_topicroute = eval(http_topicroute_result)
                    brokerDatas = http_topicroute['brokerDatas']
                    if brokerDatas is not None:
                        broker = TreeNode('brokerDatas', 'primary', 'broker info')
                        for brokerDataTmp in brokerDatas:
                            brokerdata = TreeNode(brokerDataTmp['brokerName'], 'primary', str(len(brokerDatas)+1 if brokerDatas else 0  ) +'  instrances' )
                            for (k,v) in  brokerDataTmp['brokerAddrs'].items():
                                brokeradd = TreeNode(k, "success" if k==0 else "warning", v + ( "\t Master" if k==0 else "\tSlaver" ))
                                brokerdata.add_child(brokeradd)
                            broker.add_child(brokerdata)
                        topic.add_child(broker)

                    filterServerTable = http_topicroute['filterServerTable']
                    if filterServerTable is not None:
                        filterTable = TreeNode('filterServerTable', 'primary', 'No info')
                        topic.add_child(filterTable)

                    queueDatas = http_topicroute['queueDatas']

                    if queueDatas is not None:
                        _queueDatas = TreeNode('queueDatas', 'primary', 'queue infos')
                        for queueDataTmp in queueDatas:
                            queueData = TreeNode(queueDataTmp['brokerName'], 'primary',
                                             'perm:' + str(queueDataTmp['perm']) + '\t readQueueNums:' + str(queueDataTmp[
                                                 'readQueueNums']) + '\t topicSynFlag:' + str(queueDataTmp[
                                                 'topicSynFlag']) + '\t writeQueueNums:' + str(queueDataTmp['writeQueueNums']))
                            _queueDatas.add_child(queueData)
                        topic.add_child(_queueDatas)
                    group.add_child(topic)
            result.append(group)

    return result, action_alert


